package com.hzh.cusviewdemo.Music;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;

import com.hzh.cusviewdemo.Util.Config;

public class RemoteControlClientReceiver extends BroadcastReceiver {
    @SuppressWarnings("unused")
    private static final String TAG = "HZH_ACTION_MEDIA_BUTTON";

    @Override
    public void onReceive(Context context, Intent intent) {
        //获取对应Acton，判断是否是需要的ACTION_MEDIA_BUTTON
        String action = intent.getAction();
        Log.e(Config.TAG, "收到Action:" + action);
        if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent event = (KeyEvent) intent
                    .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (event == null)
                return;

            if (event.getKeyCode() != KeyEvent.KEYCODE_HEADSETHOOK
                    && event.getKeyCode() != KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
                    && event.getAction() != KeyEvent.ACTION_DOWN)
                return;

            Intent i = null;
            switch (event.getKeyCode()) {
                /*
                 * one click => play/pause long click => previous double click =>
                 * next
                 *///这里根据按下的时间和操作，分离出具体的控制
                case KeyEvent.KEYCODE_HEADSETHOOK:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    long time = SystemClock.uptimeMillis();
                    //下面是常规的播放、暂停、停止、上下曲　
                case KeyEvent.KEYCODE_MEDIA_PLAY:
                    Log.e(Config.TAG, "From Other Device： KEYCODE_MEDIA_PLAY");
                    break;
                case KeyEvent.KEYCODE_MEDIA_PAUSE:
                    Log.e(Config.TAG, "From Other Device： KEYCODE_MEDIA_PAUSE");
                    break;
                case KeyEvent.KEYCODE_MEDIA_STOP:
                    Log.e(Config.TAG, "From Other Device： KEYCODE_MEDIA_STOP");
                    break;
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                    Log.e(Config.TAG, "From Other Device： KEYCODE_MEDIA_NEXT");
                    break;
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                    Log.e(Config.TAG, "From Other Device： KEYCODE_MEDIA_PREVIOUS");
                    break;
            }
        }
    }
}